//
//  DidscoverViewController.m
//  SimplystylePictorial
//
//  Created by Mac on 15/7/7.
//  Copyright (c) 2015年 Hu. All rights reserved.
//

#import "DiscoverViewController.h"
#import "Header.h"

@interface DiscoverViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    UITableView *_tableView;
}

@property (nonatomic,copy)NSMutableArray* infoArray;
@end
@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.infoArray = [[NSMutableArray alloc]initWithCapacity:0];
    
    [self getInfoRequest];//发送get请求
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self ;
    _tableView.dataSource = self ;
    [self.view addSubview:_tableView];

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
}
- (void)getInfoRequest
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://mmmono.com/api/category/provider?page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *array = responseObject[@"providers"];
        NSMutableArray *data =  [[NSMutableArray alloc]initWithCapacity:0];
        for(NSDictionary *dict in array)
        {
            DiscoverModel *model = [DiscoverModel getInfoWithDict:dict];
            [data addObject:model];
        }
        self.infoArray = data ;
        [_tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"sorry,网络发生错误" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertView show];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.infoArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DiscoverModel *model = self.infoArray[indexPath.row];
    
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if(!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIImageView *cellImageView = [[UIImageView alloc]init];
    cellImageView.clipsToBounds=YES;
    cellImageView.layer.cornerRadius=(kScreenW/4-30)/2.0f;
    cellImageView.layer.masksToBounds=YES;
    cellImageView.contentMode=UIViewContentModeScaleAspectFill;
    
    //保存本地图片代替头像
    NSString *fillPath = [[NSHomeDirectory() stringByAppendingString:@"Documents"]stringByAppendingString:model.avatar_large];
    UIImage *saveImage = [[UIImage alloc]initWithContentsOfFile:fillPath];
    [self saveImage:saveImage withName:model.avatar_large];

    cellImageView.frame = CGRectMake(20, 15, kScreenW/4-30, kScreenW/4-30);
    [cellImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar_large]placeholderImage:saveImage];
    [cell addSubview:cellImageView];
    
    UILabel*titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/4+10, 15, kScreenW-kScreenW/3, 30)];
    titleLabel.text=model.screen_name;
    titleLabel.font=[UIFont boldSystemFontOfSize:15];
    [cell addSubview:titleLabel];
    
    UILabel*textLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenW/4+10, 30, kScreenW-kScreenW/3, 60)];
    textLabel.text=model.descriptionTitle;
    textLabel.numberOfLines=2;
    textLabel.font=[UIFont boldSystemFontOfSize:11];
    [cell addSubview:textLabel];
    
    return cell;
}

- (void)saveImage:(UIImage *)currentImage withName:(NSString *)name
{
    NSData *data = UIImageJPEGRepresentation(currentImage, 0.3);
    NSString *fillPath = [[NSHomeDirectory() stringByAppendingString:@"Documents"]stringByAppendingString:name];
    [data writeToFile:fillPath atomically:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DiscoverInfoViewController *discoverInoVC = [[DiscoverInfoViewController alloc]init];
    DiscoverModel *model = self.infoArray[indexPath.row];
    discoverInoVC.title = model.screen_name;
    discoverInoVC.ID = model.ID;
    NSLog(@"%@",model.ID);
    discoverInoVC.hidesBottomBarWhenPushed = YES ;
    [self.navigationController pushViewController:discoverInoVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScreenW/4;
}

@end
